package com.atguigu.gulimall.order.controller;

import com.atguigu.common.tool.utils.Func;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.AllArgsConstructor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author : wenchao.long
 * @date : Created in 2021/8/18 10:15 周三
 * @description :
 */
@RestController
@AllArgsConstructor
public class RabbitController {

    private final RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public String sendMq(@RequestParam(value = "mq", defaultValue = "10") Integer mq) {
        for (int i = 0; i < mq; ++i) {
            if (i % 2 == 0) {
                OrderReturnReasonEntity reasonEntity = OrderReturnReasonEntity.builder()
                        .id(1L)
                        .createTime(new Date())
                        .name("哈哈" + i)
                        .build();
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", reasonEntity, new CorrelationData(Func.randomUUID()));
            } else {
                OrderEntity order = OrderEntity.builder()
                        .orderSn(Func.randomUUID())
                        .build();
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", order, new CorrelationData(Func.randomUUID()));
            }
        }
        return "OK";
    }
}
